﻿
using ETWJ.DTO;
using System;
using System.Collections.Generic;
using System.Text;
using ETWJ.Model;
using ETWJ.Iservice;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace ETWJ.Service
{
    public class CompanyService : CommonService<Company, CompanyDTO, MyDbContext>, ICompanyService<Company, CompanyDTO>
    {

        public async Task<CompanyDTO> GetCompanyByName(string companyName)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                var data = await ctx.Set<Company>().Where(e => e.IsDeleted == 0 && e.CompanyName == companyName).AsNoTracking().SingleOrDefaultAsync();
                return AutoMapper.Mapper.Map<CompanyDTO>(data);
            }
        }

        public async Task<int> Update(CompanyDTO dto)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                BaseService<Company> bs = new BaseService<Company>(ctx);
                var data=await bs.GetByIdAsync(dto.Id);
                data.CompanyName = dto.CompanyName;
                data.BossName = dto.BossName;
                data.PhoneNum = dto.PhoneNum;
                return await ctx.SaveChangesAsync();
            }
        }
    }
}
